<?php
/**
 * ClassName: card
 * Description:
 * @Author 周昌
 * @Create 2023/11/17 9:13
 */

namespace app;


use epii\server\Args;
use HPlus\Validate\Validate;
use libs\Tool;
use model\CardModel;
use model\MemberModel;
use service\admin\CardCateService;
use service\admin\CardPreferentialService;
use service\admin\CardRechargeService;
use service\admin\CardService;
use service\admin\HospitalService;
use service\admin\MemberService;
use think\Db;

class card extends base
{
    public function init()
    {
        parent::init();
        $this->accessDenied();
    }
    public function index()
    {
        if($this->adminInfo['role']==1){
            $this->assign('hospitals',HospitalService::getSelect());
        }
        //筛选
        $this->_as_cardCate = CardCateService::getSelect(['hospital_id'=>$this->adminInfo['hospital_id']]);
        $this->adminUiDisplay();
    }

    public function ajax_data()
    {
        $map = [];
        //登录用户权限判断
//        $hospital_id=$this->adminInfo['hospital_id'];
//        if($hospital_id){
//            $map[]=['hospital_id','=',$hospital_id];
//        }
        $hospital_group_id=$this->adminInfo['hospital_group_id'];
        if($hospital_group_id){
            $map[]=['hospital_group_id','=',$hospital_group_id];
        }

        $real_name = Args::params('real_name');
        if ($real_name) {
            $map[] = ['real_name', "LIKE", '%' . $real_name . '%'];
        }
        $phone = Args::params('phone');
        if ($phone) {
            $map[] = ['phone', "=", $phone];
        }
        $card_cate_id = Args::params('card_cate_id/d');
        if ($card_cate_id) {
            $map[] = ['card_cate_id', "=", $card_cate_id];
        }

        $hospital_id = Args::params('hospital_id');
        if ($hospital_id) {
            $map[] = ['hospital_id', "=", $hospital_id];
        }
        echo CardService::getAjaxData($map);
    }

    public function add_edit()
    {
        $id = Args::params('id');
        if ($_SERVER['REQUEST_METHOD'] == "POST") {
            $data = Args::params('r');
            //登录权限判断
            $hospital_id=$this->adminInfo['hospital_id'];
            if(!empty($hospital_id)){
                $data['hospital_id']=$hospital_id;
                $data['hospital_group_id']=$this->adminInfo['hospital_group_id'];
            }
            //验证
            $this->validate($data,[
                'phone|用户手机'=>'require|mobile',
                'real_name|姓名'=>'require|chsAlpha',
//                'member_id|用户id'=>'|integer|>=:0',
                'sex|性别'=>'require|integer',
                'hospital_id|所属医院'=>'require|integer|>=:0',
                'card_cate_id|卡分类'=>'require|integer|>=:0',
                'status|状态'=>'require|integer',
                'recharge_money|充值金额'=>'integer|>=:0',
                'gift_money|赠送金额不能为空可填0'=>'integer|>=:0',
            ]);
            //医院组内，一种卡只能办理一张
            //新增
            $member_id=MemberModel::getValue(['phone'=>$data['phone']],'id');
            if($id){
                //修改
                $where=[
                    ['hospital_group_id','=',$this->adminInfo['hospital_group_id']],
                    ['card_cate_id','=',$data['card_cate_id']],
                    ['member_id','=',$member_id],
                    ['id','<>',$id],
                ];
                $count=CardModel::getCount($where);
            }else{
                if($member_id){
                    $where=[
                        ['hospital_group_id','=',$this->adminInfo['hospital_group_id']],
                        ['card_cate_id','=',$data['card_cate_id']],
                        ['member_id','=',$member_id],
                    ];
                    $count=CardModel::getCount($where);
                }else{
                    $count=0;
                }

            }
            if($count>0){
                $this->error('同类型卡只能办理一张');
            }
            $res=CardService::addEdit($data,$id);
            $res['code']?$this->success($res['msg']):$this->error($res['msg']);
        }
        $this->_as_info = $id ? CardModel::getInfo(['id'=>$id]) : [];
        $this->_as_id = $id;
        $this->_as_sex =MemberService::memberSexSelect();
        $this->_as_status = CardService::statusSelect();
        if($this->adminInfo['role']==1){
            $this->_as_cardCate = CardCateService::getSelect();
        }else{
            if ($id){
                $this->_as_cardCate = CardCateService::getSelect(['hospital_group_id'=>$this->adminInfo['hospital_group_id']]);

            }else{
                $this->_as_cardCate = CardCateService::getSelect(['hospital_group_id'=>$this->adminInfo['hospital_group_id'],'status'=>CardCateService::$status_index['yes']]);
            }
        }


        $this->adminUiDisplay();
    }
    //修改状态
    public function status(){
        $id=Args::params('id/d/1','id,不能为空');
        $status=Args::params('status');
        $res=CardService::status($id,$status);
        if($res['code']){
            $this->success('成功','refresh');
        }
        $this->error();
    }

    /**
     * @return array|false|string* 删除
     */
    public function del()
    {
        $id = Args::params('id');
        //todo 判断分类下是否已经有，无则可以删除
        $res = CardService::del($id);
        if ($res['code']) {
            $this->success($res['msg'],'refresh');
        } else {
            $this->error($res['msg']);
        }
    }

    /**
     * #User: Administrator
     * #Time: 2023/11/17 11:29
     * #Author:周昌
     * #Description: 通过手机号获取用户信息
     */
    public function getMemberInfo()
    {
        $phone=Args::params('phone/1','手机号不能为空');
        $info=MemberModel::getInfo(['phone'=>$phone],'id,phone,real_name,sex');
        $res= $info?Tool::Success('成功',$info):Tool::Error();
        exit(json_encode($res));
    }
    //获取充值赠送金额
    public function getGiftMoney()
    {
        $card_cate_id=Args::params('card_cate_id');
        $recharge_money=Args::params('recharge_money');
        $validate=new Validate();
        $validate->make([
            'card_cate_id|会员卡分类id'=>'require|integer|>:0',
            'recharge_money|充值金额'=>'require|integer|>:0',
        ]);
        if(!$validate->check(['card_cate_id'=>$card_cate_id,'recharge_money'=>$recharge_money])){
            exit(json_encode(Tool::Error($validate->getError())));
        }
        $gift_money=CardPreferentialService::getGiftByRecharge($card_cate_id,$recharge_money);
        exit(json_encode(Tool::Success('成功',['gift_money'=>$gift_money])));
    }
    //充值
    public function recharge()
    {
        $id = Args::params('id');
        if ($_SERVER['REQUEST_METHOD'] == "POST") {
            $data = Args::params('r');
            //验证
            $this->validate($data,[
                'recharge_money|充值金额'=>'require|integer|>=:0',
                'gift_money|赠送金额不能为空可填0'=>'require|integer|>=:0',
            ]);
            $res = CardService::cardRecharge(['id'=>$id],$data['recharge_money'],$data['gift_money']);
            $res['code']?$this->success($res['msg']):$this->error($res['msg']);
        }
        $this->_as_info = $id ? CardModel::getInfo(['id'=>$id]) : [];
        $this->_as_id = $id;
        $this->_as_status = CardService::statusSelect();
        if($this->adminInfo['role']==1){
            $this->_as_cardCate = CardCateService::getSelect();
        }else{
            $this->_as_cardCate = CardCateService::getSelect(['hospital_group_id'=>$this->adminInfo['hospital_group_id']]);
        }


        $this->adminUiDisplay();
    }

}